package com.ruoyi.system.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.SysBusiness;
import com.ruoyi.system.domain.SysEnterpriseCertification;
import com.ruoyi.system.domain.SysParking;
import com.ruoyi.system.domain.parm.SysParkingAddParm;
import com.ruoyi.system.domain.parm.SysParkingParm;
import com.ruoyi.system.service.ISysBusinessService;
import com.ruoyi.system.service.ISysParkingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.UUID;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2024-03-24
 */

@Api(tags = "商家停车场")
@RestController
@RequestMapping("/bus/parking")
public class BusParkingController extends BaseController
{
    @Autowired
    private ISysParkingService sysParkingService;
    @Autowired
    private ISysBusinessService sysBusinessService;
    /**
     * 查询【请填写功能名称】列表
     */
    @ApiOperation(value = "商家停车场list")
    @GetMapping("/list")
    public TableDataInfo list(SysParkingParm sysParking) throws Exception {
        SysBusiness bu = sysBusinessService.getUserInfo();
        if(bu==null){
            throw new Exception("用户未登陆");
        }
        sysParking.setBusinessId(bu.getId() );

        startPage();
        List<SysParking> list = sysParkingService.selectSysParkingList(sysParking);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @ApiOperation(value = "商家停车场导出")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysParkingParm sysParking) throws Exception {
        SysBusiness bu = sysBusinessService.getUserInfo();
        if(bu==null){
            throw new Exception("用户未登陆");
        }
        sysParking.setBusinessId(bu.getId() );
        List<SysParking> list = sysParkingService.selectSysParkingList(sysParking);
        ExcelUtil<SysParking> util = new ExcelUtil<SysParking>(SysParking.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @ApiOperation(value = "商家停车场详情")
    @GetMapping(value = "/info")
    public AjaxResult getInfo(@RequestParam(name = "id",required = true) String id) throws Exception {
        SysBusiness bu = sysBusinessService.getUserInfo();
        if(bu==null){
            throw new Exception("用户未登陆");
        }


        return success(sysParkingService.selectSysParkingById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @ApiOperation(value = "商家添加停车场（停车场名称可重复）")
    @PostMapping("add")
    public AjaxResult add(@RequestBody @Valid SysParkingAddParm sysParkingAddParm) throws Exception {

        SysBusiness bu = sysBusinessService.getUserInfo();
        if(bu==null){
            throw new Exception("用户未登陆");
        }

        SysParking sysParking = new SysParking();
        BeanUtils.copyProperties(sysParkingAddParm, sysParking);

        sysParking.setBusinessId(bu.getId() );
        sysParking.setCreateBy(bu.getUserName() );
        sysParking.setStatus(1);
        sysParking.setId(UUID.randomUUID().toString());
        return toAjax(sysParkingService.insertSysParking(sysParking));
    }

    /**
     * 修改【请填写功能名称】
     */
    @ApiOperation(value = "商家修改停车场")
    @PutMapping("edit")
    public AjaxResult edit(@RequestBody @Valid SysParkingAddParm sysParkingAddParm) throws Exception {
        SysBusiness bu = sysBusinessService.getUserInfo();
        if(bu==null){
            throw new Exception("用户未登陆");
        }

        SysParking sysParking = new SysParking();
        BeanUtils.copyProperties(sysParkingAddParm, sysParking);
        if(sysParking.getId()==null){
            throw new Exception("id不能为空");
        }
        sysParking.setUpdateBy(bu.getUserName() );

        return toAjax(sysParkingService.updateSysParking(sysParking));
    }

    /**
     * 删除【请填写功能名称】
     */
    @ApiOperation(value = "商家删除停车场")
	@DeleteMapping("/delete/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) throws Exception {
        SysBusiness bu = sysBusinessService.getUserInfo();
        if(bu==null){
            throw new Exception("用户未登陆");
        }

        return toAjax(sysParkingService.deleteSysParkingByIds(ids,bu.getId()));
    }


}
